package com.dense.kuiniu.floatcabinet.mvp.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ScreenUtils;
import com.dense.kuiniu.floatcabinet.R;
import com.dense.kuiniu.floatcabinet.utils.timer.CountDownView;
import com.dense.kuiniu.floatcabinet.utils.timer.CounterController;

import timber.log.Timber;

/**
 * @author Administrator
 * @date 2020/10/28
 */
public class DialogPutCard extends Dialog {
    private EditText mEtCardNo;
    private boolean hasRead = false;
    private OnCardReadListener mOnCardReadListener;
    private CountDownView mCountDownView;

    public DialogPutCard(@NonNull Context context) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_put_card, null);
        mEtCardNo = view.findViewById(R.id.et_card_no);
        mEtCardNo.setInputType(InputType.TYPE_NULL);
        mEtCardNo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Timber.e(s.toString());
                if (s.length() >= 10) {
                    if (!hasRead) {
                        hasRead = true;
                        String cardNo = mEtCardNo.getText().toString().replace("\n", "");
                        if (mOnCardReadListener != null) {
                            mOnCardReadListener.onComplete(cardNo);
                        }
                        exit();
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mCountDownView = view.findViewById(R.id.view_count_down);
        setContentView(view);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                mEtCardNo.setText("");
                hasRead = false;
                if (mCountDownView != null) {
                    mCountDownView.stop();
                }
            }
        });
    }

    public void show(OnCardReadListener listener) {
        super.show();
        measureSize();
        if (mCountDownView != null) {
            mCountDownView.start(new CounterController.OnCompleteListener() {
                @Override
                public void onComplete() {
                    exit();
                }
            });
        }
        mOnCardReadListener = listener;
    }

    protected void measureSize() {
        Window dialogWindow = getWindow();
        if (dialogWindow != null) {
            dialogWindow.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.shape_transparent));
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = (int) (ScreenUtils.getScreenWidth() * 0.65); // 宽度
            lp.height = (int) (ScreenUtils.getScreenHeight() * 0.315); // 高度
            dialogWindow.setGravity(Gravity.CENTER);
            dialogWindow.setAttributes(lp);
            dialogWindow.setWindowAnimations(R.style.DialogOutAndInStyle);
        }
    }

    private void exit() {
        dismiss();
    }

    public interface OnCardReadListener {
        void onComplete(String cardNo);
    }
}